/*
 * Powered By fanrenqij.com
 * Since 2017 - 2017
 */

package com.fanrenqiji.modules.business.semester.web;


import com.fanrenqiji.common.config.GlobalSetting;
import com.fanrenqiji.common.persistence.Page;
import com.fanrenqiji.common.web.BaseController;
import com.fanrenqiji.modules.business.course.vo.CourseVO;
import com.fanrenqiji.modules.business.semester.vo.SemesterPageVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Calendar;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.fanrenqiji.modules.business.semester.vo.SemesterVO;
import com.fanrenqiji.modules.business.semester.service.SemesterService;

@Controller
@RequestMapping(value = "${adminPath}/business/semester")
public class SemesterController extends BaseController {

    private static Logger log = LoggerFactory.getLogger(SemesterController.class);
    @Autowired
    private SemesterService semesterService;

    @ModelAttribute("semester")
    public SemesterVO getSemester(@RequestParam(required = false) String id) {
        if (StringUtils.isNotEmpty(id)) {
            return semesterService.get(id);
        } else {
            return new SemesterVO();
        }
    }

    /**
     * 列表
     *
     * @return
     */
    @RequiresPermissions("business:semester:view")
    @RequestMapping(value = {"list", ""})
    public String list(SemesterVO semester, HttpServletRequest request,
                       HttpServletResponse response, Model model) {
        Page<SemesterVO> page = semesterService.findSemesterByPage(new Page<SemesterVO>(request, response),
                semester);
        model.addAttribute("page", page);
        model.addAttribute("semester", semester);
        return "modules/business/semester/semesterList";
    }

    /**
     * 选择列表
     *
     * @return
     */
    @RequestMapping(value = "select")
    public String select(SemesterVO semester, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<SemesterVO> page = semesterService.findSemesterByPage(new Page<SemesterVO>(request, response, 10), semester);
        model.addAttribute("page", page);
        model.addAttribute("semester", semester);
        return "modules/business/semester/semesterSelect";
    }

    /**
     * 用户添加更新表
     *
     * @param model
     * @return
     */
    @RequiresPermissions("business:semester:view")
    @RequestMapping(value = "form")
    public String form(Model model, SemesterVO semester) {
        if (StringUtils.isNotEmpty(semester.getId())) {
            model.addAttribute("semester", semesterService.get(semester.getId()));
        } else {
            model.addAttribute("semester", semester);
        }
        return "modules/business/semester/semesterForm";
    }

    /**
     * 添加或修改提交
     *
     * @param semester
     * @param model
     * @param redirectAttributes
     * @return
     */
    @RequiresPermissions("business:semester:edit")
    @RequestMapping(value = "save")
    public String save(SemesterVO semester, Model model,
                       RedirectAttributes redirectAttributes) {// TODO @Valid 添加后端校验
        if (!beanValidator(model, semester)) {
            return form(model, semester);
        }

        semesterService.update(semester);
        addMessage(redirectAttributes, "保存成功");
        return "redirect:" + GlobalSetting.getAdminPath()
                + "/business/semester/?repage";
    }

    /**
     * 删除操作
     *
     * @return
     */
    @RequestMapping("delete")
    @RequiresPermissions("business:semester:edit")
    public String delete(String id, RedirectAttributes redirectAttributes) {
        semesterService.delete(id);
        addMessage(redirectAttributes, "删除成功");
        return "redirect:" + GlobalSetting.getAdminPath() + "/business/semester/";
    }

    /**
     * 开始上课的方法
     *
     * @param semesterVO
     */
    @RequestMapping("begin")
    public void begin(SemesterVO semesterVO) {
        semesterService.begin(semesterVO);
    }
}